package com.sayimo.maker.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.mall.orders.dao.OrdersFlowInterfaceDao;
import com.sayimo.maker.mall.orders.dao.OrdersListInterfaceDao;
import com.sayimo.maker.mall.orders.service.OrdersListInterfaceService;
import com.sayimo.maker.mall.orders.vo.OrdersFlow;
import com.sayimo.maker.mall.orders.vo.OrdersList;

/**
 * @package com.sayimo.maker.task
 * @author gongyijun
 * @Title:StatisTask
 * @Description:
 * @date 2017年3月13日
 */
@Component("statisTask")
public class StatisTask 
{

	private static Logger logger = LogManager.getLogger(StatisTask.class.getName());
	@Resource(name = "ordersListInterfaceDao")
	private OrdersListInterfaceDao ordersDao;
	@Resource(name = "ordersFlowInterfaceDao")
	private OrdersFlowInterfaceDao ordersFlowDao;
	@Resource(name = "ordersListInterfaceService")
	private OrdersListInterfaceService ordersListInterfaceService;
	private HttpServletRequest request;

	/**
	 * @Package com.sayimo.school.mall.module.task
	 * @Author 龚益君
	 * @MethodName:changeStatusShippedOrder
	 * @Description:更改已经发货未点击收货的订单
	 * @throws ServiceException
	 * @Date 2016年9月28日,下午3:28:19
	 */
	@SuppressWarnings("unchecked")
	public void changeStatusShippedOrder() throws ServiceException {
		try {
			logger.info("begin");
			List<OrdersFlow> ordersFlowList = ordersFlowDao.selectShippedOrder();
			for (OrdersFlow ordersFlow : ordersFlowList) {
				logger.info("action");
				Integer id = ordersFlow.getOrdersId();
				OrdersList order = ordersDao.queryOrdersListById(id);
				Date date = new Date();
				Long num = date.getTime() - ordersFlow.getActionDate().getTime() - 1000L * 60L * 60L * 24L * 15L;
				if (num >= 0) {
					Map<String, Object> paramMaps = new HashMap<>();
					paramMaps.put("orderId", id);
					paramMaps.put("customerId", order.getCustomerId());
					ordersListInterfaceService.updateConfirmReceive(paramMaps);
				}
			}
		} catch (DaoException de) {
			logger.debug(de);
			throw new ServiceException(de.getMessage());
		}

	}
	
	/**
	* @Package com.sayimo.maker.task
	* @Author 谭新
	* @MethodName: unconfirmOrderTransferProvider
	* @Description:未确认订单到时间后自动转账给供应商
	* @return void
	* @Date 2017年4月12日,下午3:14:08
	*/
	@SuppressWarnings("unchecked")
	public void unconfirmOrderTransferProvider() throws ServiceException {
		try {
			logger.info("begin");
			List<OrdersFlow> ordersFlowList = ordersFlowDao.selectShippedOrder();
			for (OrdersFlow ordersFlow : ordersFlowList) {
				logger.info("action");
				Integer id = ordersFlow.getOrdersId();
				Date date = new Date();
				Long num = date.getTime() - ordersFlow.getActionDate().getTime() - 1000L * 60L * 60L * 24L * 3L;
				if (num >= 0) {
					Map<String, Object> paramMaps = new HashMap<>();
					paramMaps.put("orderId", id);
					ordersListInterfaceService.unconfirmOrderTransferProvider(paramMaps);
				}
			}
		} catch (DaoException de) {
			logger.debug(de);
			throw new ServiceException(de.getMessage());
		}

	}


}
